﻿// -----------------------------------------------------------------------
// <copyright file="AcknowledgementPapService.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Acknowledgement
{
	using System;
	using System.Collections.Specialized;
	using Microsoft.Practices.Unity;
	using Zen.BlackBerry.Common;
	using Zen.BlackBerry.Configuration;
	using Zen.BlackBerry.Pap;
	using Zen.BlackBerry.Pap.Parser;

	/// <summary>
	/// Extension of the pap package <see cref="T:PapService"/> to include
	/// result notification for the acknowledgement package. 
	/// </summary>
	/// <remarks>
	/// The OnNotification() method retrieves the push application id from the
	/// HTTP request, creates a Notification object that is constructed based
	/// on the ResultNotification and the push application id and passes the
	/// object to the NotificationProcessorService for further processing. 
	/// </remarks>
	public class AcknowledgementPapService : PapService
	{
		private INotificationProcessorService _notificationProcessorService;

		/// <summary>
		/// Initializes a new instance of the <see cref="AcknowledgementPapService"/> class.
		/// </summary>
		/// <param name="pushProperties">The push properties.</param>
		/// <param name="notificationProcessorService">The notification processor service.</param>
		public AcknowledgementPapService(
			[Dependency]IPushProperties pushProperties,
			[Dependency]INotificationProcessorService notificationProcessorService)
			: base(pushProperties)
		{
			_notificationProcessorService = notificationProcessorService;
		}

		/// <summary>
		/// Called by the service to process the specified inbound notification.
		/// </summary>
		/// <param name="resultNotification">The result notification.</param>
		/// <param name="notificationParameters">The notification parameters.</param>
		protected override void OnNotification(
			ResultNotification resultNotification,
			NameValueCollection notificationParameters)
		{
			//if (log.isDebugEnabled())
			//{
			//    log.debug("resultNotification=" + resultNotification);
			//    Set keys = notificationParameters.keySet();
			//    StringBuffer msg = new StringBuffer("");
			//    foreach(String key in keys)
			//    {
			//        msg.append("key=" + key);
			//        msg.append(", values=|");
			//        for (String value : (String[])notificationParameters.get(key))
			//        {
			//            msg.append(value + "|");
			//        }
			//        msg.append("; ");
			//    }
			//    log.debug("notificationParameters=" + msg);
			//}

			string[] appIdParameterValues =
				notificationParameters.GetValues("appid");
			if (appIdParameterValues == null)
			{
				throw new PushSDKException("Notification did not contain a push application id.");
			}
			if (appIdParameterValues.Length != 1)
			{
				throw new PushSDKException(string.Format(
					"Notification did not contain only one value for the push application id. Number of values={0}.", appIdParameterValues.Length));
			}

			string pushAppId = appIdParameterValues[0];
			if (string.IsNullOrWhiteSpace(pushAppId))
			{
				throw new PushSDKException("Notification contained a null or empty push application id.");
			}

			int? addressCount = null;
			string[] addressCountParameterValues = notificationParameters
				.GetValues("address_count");
			if (addressCountParameterValues != null)
			{
				if (addressCountParameterValues.Length == 1)
				{
					addressCount = Int32.Parse(addressCountParameterValues[0]);
				}
				else
				{
					throw new PushSDKException("There should be only one address_count parameter");
				}
			}

			Notification notification = new Notification(resultNotification, pushAppId);
			notification.AddressCount = addressCount;
			//if (log.isDebugEnabled())
			//{
			//    log.debug("notification=" + notification);
			//}
			_notificationProcessorService.AddNotification(notification);
		}
	}
}
